#Boa:Dialog:ComRulesEdition

import wx
import wx.grid

from dto import Rule
import ruleManager

import ruleSpecEditorFactory

import sys

class SpecDialog(wx.Dialog):
    def __init__(self, par):
        wx.Dialog.__init__(self, id=wx.NewId(), name='', parent=par,
              pos=wx.Point(353, 49), size=wx.Size(904, 598),
              style=wx.DEFAULT_DIALOG_STYLE, title='Editar Reglas')
        self.SetClientSize(wx.Size(888, 560)) 

def create(parent):
    return ComRulesEdition(parent)

[wxID_COMRULESEDITION, wxID_COMRULESEDITIONBORRAR, 
 wxID_COMRULESEDITIONBUTTON1, wxID_COMRULESEDITIONCOMBOBOX1, 
 wxID_COMRULESEDITIONCOMPONENTES, wxID_COMRULESEDITIONGUARDAR, 
 wxID_COMRULESEDITIONLISTCTRL1, wxID_COMRULESEDITIONMONTHLBL, 
 wxID_COMRULESEDITIONNOMBRE, wxID_COMRULESEDITIONPUNTOS, 
 wxID_COMRULESEDITIONSTATICTEXT1, wxID_COMRULESEDITIONSTATICTEXT2, 
 wxID_COMRULESEDITIONTEXTNAME, wxID_COMRULESEDITIONTEXTPOINTS, 
 wxID_COMRULESEDITIONYEARLBL, 
] = [wx.NewId() for _init_ctrls in range(15)]

class ComRulesEdition(wx.Dialog):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Dialog.__init__(self, id=wxID_COMRULESEDITION, name='', parent=prnt,
              pos=wx.Point(126, 188), size=wx.Size(1085, 463),
              style=wx.DEFAULT_DIALOG_STYLE, title='Editar reglas')
        self.SetClientSize(wx.Size(1069, 425))

        self.Nombre = wx.StaticText(id=wxID_COMRULESEDITIONNOMBRE,
              label='Nombre:', name='Nombre', parent=self, pos=wx.Point(24, 64),
              size=wx.Size(42, 13), style=0)

        self.Puntos = wx.StaticText(id=wxID_COMRULESEDITIONPUNTOS,
              label='Puntos:', name='Puntos', parent=self, pos=wx.Point(24, 88),
              size=wx.Size(38, 13), style=0)

        self.textName = wx.TextCtrl(id=wxID_COMRULESEDITIONTEXTNAME,
              name='textName', parent=self, pos=wx.Point(112, 64),
              size=wx.Size(320, 21), style=0, value='')

        self.textPoints = wx.TextCtrl(id=wxID_COMRULESEDITIONTEXTPOINTS,
              name='textPoints', parent=self, pos=wx.Point(112, 88),
              size=wx.Size(100, 21), style=0, value='')

        self.comboBox1 = wx.ComboBox(choices=[],
              id=wxID_COMRULESEDITIONCOMBOBOX1, name='comboBox1', parent=self,
              pos=wx.Point(24, 144), size=wx.Size(130, 21),
              style=wx.CB_READONLY, value='comboBox1')

        self.button1 = wx.Button(id=wxID_COMRULESEDITIONBUTTON1,
              label='Agregar', name='button1', parent=self, pos=wx.Point(160,
              144), size=wx.Size(75, 23), style=0)
        self.button1.Bind(wx.EVT_BUTTON, self.OnAddSpecButton,
              id=wxID_COMRULESEDITIONBUTTON1)

        self.Borrar = wx.Button(id=wxID_COMRULESEDITIONBORRAR, label='Borrar',
              name='Borrar', parent=self, pos=wx.Point(976, 184),
              size=wx.Size(75, 23), style=0)
        self.Borrar.Bind(wx.EVT_BUTTON, self.OnDeleteSpecButton,
              id=wxID_COMRULESEDITIONBORRAR)

        self.Componentes = wx.StaticBox(id=wxID_COMRULESEDITIONCOMPONENTES,
              label='Componentes', name='Componentes', parent=self,
              pos=wx.Point(16, 120), size=wx.Size(1048, 224), style=0)

        self.Guardar = wx.Button(id=wxID_COMRULESEDITIONGUARDAR,
              label='Guardar', name='Guardar', parent=self, pos=wx.Point(984,
              360), size=wx.Size(75, 23), style=0)
        self.Guardar.Bind(wx.EVT_BUTTON, self.OnSaveButton,
              id=wxID_COMRULESEDITIONGUARDAR)

        self.listCtrl1 = wx.ListCtrl(id=wxID_COMRULESEDITIONLISTCTRL1,
              name='listCtrl1', parent=self, pos=wx.Point(24, 184),
              size=wx.Size(944, 152), style=wx.LC_REPORT)

        self.staticText1 = wx.StaticText(id=wxID_COMRULESEDITIONSTATICTEXT1,
              label='A\xf1o:', name='staticText1', parent=self, pos=wx.Point(24,
              16), size=wx.Size(24, 13), style=0)

        self.staticText2 = wx.StaticText(id=wxID_COMRULESEDITIONSTATICTEXT2,
              label='Mes:', name='staticText2', parent=self, pos=wx.Point(24,
              40), size=wx.Size(24, 13), style=0)

        self.yearLbl = wx.StaticText(id=wxID_COMRULESEDITIONYEARLBL,
              label='staticText3', name='yearLbl', parent=self,
              pos=wx.Point(112, 16), size=wx.Size(55, 13), style=0)

        self.monthLbl = wx.StaticText(id=wxID_COMRULESEDITIONMONTHLBL,
              label='staticText4', name='monthLbl', parent=self,
              pos=wx.Point(112, 40), size=wx.Size(55, 13), style=0)

    def __init__(self, parent, year, month, rule):
        self._init_ctrls(parent)
        
        self.rule = rule
        if not self.rule:
            self.rule = ruleManager.new_rule(year, month)
                   
        self.specs = self.rule.specs
            
        self.textName.SetValue(self.rule.name)
        self.textPoints.SetValue(str(self.rule.points))        
        
        self.yearLbl.SetLabel(str(year))
        self.monthLbl.SetLabel(str(month))
        
        self.createSpecsGrid()
        self.populateSpecsCombo()
        self.populateSpecsGrid()
        
    def createSpecsGrid(self):
        columnas =['Restriccion', 'Elementos', 'Cantidad', 'Anio', 'Mes', 'Desde', 'Hasta']              
        for i, col in enumerate(columnas):
            self.listCtrl1.InsertColumn(i, heading=col)
            self.listCtrl1.SetColumnWidth(i, 60)
        
        self.listCtrl1.SetColumnWidth(0, 130)
        self.listCtrl1.SetColumnWidth(1, 450)
        
    def populateSpecsCombo(self):
        self.comboBox1.Clear()
        for st in ruleManager.get_rule_spec_types():
            self.comboBox1.Append(st)
            
        self.comboBox1.Select(0)
    
    def populateSpecsGrid(self):
        self.listCtrl1.DeleteAllItems()
        for i, r in enumerate(self.specs):  
            index = self.listCtrl1.InsertStringItem(i, r.type)
            self.listCtrl1.SetStringItem(index, 1, ", ".join(r.elements), 0)
            self.listCtrl1.SetStringItem(index, 2, str(r.amount), 0)
            self.listCtrl1.SetStringItem(index, 3, str(r.year), 0)
            self.listCtrl1.SetStringItem(index, 4, str(r.month), 0)
            self.listCtrl1.SetStringItem(index, 5, str(r.f), 0)
            self.listCtrl1.SetStringItem(index, 6, str(r.t), 0)
            
    def GetRule(self):
        return self.rule
    
    def GetSelectedComboSubrule(self):
        key = self.comboBox2.GetValue()
        r = self.combo_subrules[key]
        return r
    
    def GetSelectedGridSpec(self):
        selected_row = self.listCtrl1.GetFirstSelected()
        s = self.specs[selected_row]
        return s

    def OnAddSpecButton(self, event):
        spec_type = self.comboBox1.GetValue()
        editor = ruleSpecEditorFactory.drawForm(self, spec_type)
        editor.ShowModal()
        rs = editor.rule_spec
        self.specs.append(rs)
        self.populateSpecsGrid()

    def OnDeleteSpecButton(self, event):
        s = self.GetSelectedGridSpec()
        self.specs.remove(s)
        self.populateSpecsGrid()

    def OnSaveButton(self, event):
        self.rule.name = self.textName.GetValue()
        self.rule.points = self.textPoints.GetValue()
        self.rule.specs = self.specs
        
        self.rule = ruleManager.store_rule(self.rule)
        self.Close()
